<?php
namespace Home\Controller;

use Think\Controller;

class CartController extends HomeController
{
    public function __construct()
    {
        parent::__construct();

        //判断是否登录
        hook('login','','true');

        //底部菜单设置
        $menus = C("MENUS");
        $menus[2] = array(
            'href'      =>  U('/Cart/index'),
            'li_class'  =>  'margin-r',
            'src'       =>  C('TMPL_PARSE_STRING')['__IMG__'].'/menu-33.png',
            'p_class'   =>  'color-menu',
            'text'      =>  '购物车'
        );
        C("MENUS",$menus);
    }
    public function index(){
        $uid = session("member.id");
        if(!$uid){
            //redirect(U('Login/index'));
			//redirect(U('Login/index',array('acturl'=>"http://".$_SERVER['HTTP_HOST'].__SELF__)));
        }
        $Cart = D("Cart");
        $carts = $Cart->getCart($uid);
        $Goods = D("Goods");
        foreach($carts as &$v){
            $v['goods'] =   $Goods->getDetail($v['goods_id']);
        }
        $this->assign('carts',$carts);

        $total = 0;
        foreach($carts as $v2){ 
            $total += $v2['number']*$v2['goods']['price'];
        }
        $this->assign('total',$total);

        $this->display();
    }

    public function edit(){
        $uid = session("member.id");
        if(!$uid){
            //redirect(U('Login/index'));
            //redirect(U('Login/index',array('acturl'=>"http://".$_SERVER['HTTP_HOST'].__SELF__)));
        }
        $Cart = D("Cart");
        $carts = $Cart->getCart($uid);
        $total = 0;
        foreach($carts as $v){
            $total += $v['number']*$v['price'];
        }
        $this->assign('total',$total);
        trace($carts,'carts');
        trace($Cart->getLastSql(),'sql');
        $Goods = D("Goods");
        foreach($carts as &$v){
            $v['goods'] =   $Goods->getDetail($v['goods_id']);
        }

        $this->assign('carts',$carts);
        $this->display();
    }
    
    public function update(){
        $uid = session("member.id");
        if(!$uid){
			redirect(U('Login/index',array('acturl'=>urlencode($_SERVER["HTTP_REFERER"]))));
        }
        $id = I("id",0);
        $goods_id = I("goods_id",0);
        $goods_title = I("goods_title",'商品');
        $price  =   I("price",0);

        if(I("activity",0)==0){
            $number = I("number",1);
        }else{
            $number = 0;
        }

        $user_id = session("member.id");
        $data = array(
            'id'        =>$id,
            'user_id'=>$user_id,
            'goods_id'  =>  $goods_id,
            'goods_title'=>$goods_title,
            'price' =>  $price,
            'number'    =>  $number,
            'attribute' =>$_POST['attribute'],
            'activity'=>I("activity",0),
            'tags'=>$_POST['tags']
        );
//        $data = array_filter($data);
//        dump($data); 

        trace($data,'data');
        $Cart = D("Cart");
        if($Cart->update($data)){
//            $this->success('添加成功');
//            redirect(U("Cart/index"));
            $this->alert("添加成功",$_SERVER['HTTP_REFERER'],1);
        }else{
            echo 'add failed';
            echo $Cart->getError();
            //$this->error('添加失败');
        }

    }
    public function ajax_number(){
        if( !(0<session("member.id"))){
            $this->ajaxReturn(array('code'=>0,'msg'=>'您未登录'));
        }
        $data['id'] = I("id",0);
        $data['number'] = I("number",0);
        $data['activity'] = I("activity",0);

        if( 0 == $data['id'] ){
            $this->ajaxReturn(array('code'=>0,'msg'=>'不存在购物车'));
        }
        if( 0 < $data['activity'] ){
            $this->ajaxReturn(array('code'=>0,'msg'=>'活动商品限购数量'));
        }

        $Cart = D("Cart");
        if($Cart->where(array('id'=>$data['id']))->save($data)){
            $this->ajaxReturn(array('code'=>1));
        }else{
            $this->ajaxReturn(array('code'=>0,'msg'=>"保存失败 ".$Cart->getError()));
        }

    }
    public function ajax_update(){
        dump($_POST);
        $id = I("id",0);
        $goods_id = I("goods_id",0);
        if(!$goods_id){
            $this->ajaxReturn(array('status'=>0,'msg'=>'商品ID为空'));
        }
        $price  =   I("price",0);
        $number = I("number",1);
        $user_id = session("member.id");
        $data = array(
            'id'        =>$id,
            'user_id'=>$user_id,
            'goods_id'  =>  $goods_id,
            'price' =>  $price,
            'number'    =>  $number
        );
        $data = array_filter($data);
        dump($data);
        trace($data);
        $Cart = D("Cart");
        if($Cart->update($data)){
//            $this->success('添加成功');
            $this->ajaxReturn(array('status'=>1));
        }else{
            $this->ajaxReturn(array('status'=>0,'msg'=>$Cart->getError()));
            //$this->error('添加失败');
        }
    }
    public function delete(){
        $uid = session("member.id");
        if(!$uid){
            $this->alert("需要登录",$_SERVER['HTTP_REFERER']);
        }
        $id = I('cart_id',0);
        $Cart = D("Cart");
        if($Cart->del($id,$uid)){
            $this->alert('删除成功',$_SERVER['HTTP_REFERER'],0);
        }else{
            $this->alert("删除失败 ".$Cart->getError(),$_SERVER['HTTP_REFERER']);
        }
    }

    /**
     * 提示跳转
     */
    public function alert($msg,$url,$time=-1) {
        $this->assign('info',array('msg'=>$msg,'url'=>$url));
        $this->assign("time",$time);
        $this->display('Notice/location');
        die();
    }
}
